import React from 'react';
import {
  ActivityIndicator,
  StatusBar,
  StyleSheet,
  View,
  Image
} from 'react-native';

import Axios from '../../axios';
import StorageUtil from '../../common/storageUtil';

export default class AuthLoadingScreen extends React.Component {
  constructor() {
    super();
    this._bootstrapAsync();
  }

  _bootstrapAsync = async () => {
    const userToken = await StorageUtil.getItem(global.Constants.UESR_TOKEN);

    if( userToken ){
      Axios.ajax({
  			url:'/validToken'
  		}).then((res)=>{
  			//console.log("validToken="+JSON.stringify(res));
        //console.log("USERPRIVILEGED="+JSON.stringify(JSON.parse(res.USERPRIVILEGED)));
        //console.log("USERINFO="+JSON.stringify(JSON.parse(res.USERINFO)));

        const userprivileged = JSON.parse(res.USERPRIVILEGED);
        const userInfo = JSON.parse(res.USERINFO);
        const userCollect = JSON.parse(res.USER_COLLECT);

        StorageUtil.setItem(global.Constants.USER_ROLEDATA, userprivileged.roleDataVos);
        StorageUtil.setItem(global.Constants.AREADIC_LIST, userprivileged.areaDicList);
        StorageUtil.setItem(global.Constants.CITYDIC_LIST, userprivileged.cityDicList);
        StorageUtil.setItem(global.Constants.PROVINCEDIC_LIST, userprivileged.provinceDicList);
        StorageUtil.setItem(global.Constants.DEPART, userprivileged.departVo);
        StorageUtil.setItem(global.Constants.USER_INFO, userInfo);
        StorageUtil.setItem(global.Constants.USER_COLLECT, userCollect);

  		})
      this.props.navigation.navigate('App');
    }else{
      this.props.navigation.navigate('Auth');
    }

    //this.props.navigation.navigate(userToken ? 'App' : 'Auth');
  };

  render() {
    return (
      <View style={styles.container}>
        <ActivityIndicator animating={false}/>
        <StatusBar barStyle="default" networkActivityIndicatorVisible={true}/>
        <Image
          style={styles.sideBarLogo}
          source={require('../../assets/images/logo.png')}
        />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
  sideBarLogo:{
		width:80,
		height:80
	},
});
